package com.kuang.builder;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import com.kuang.bean.SqlTableInfo;
import com.kuang.constants.DaoConstants;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.nio.charset.Charset;

/**
 * @ClassName BuildPo
 * @Description TODO
 * @Author Augenestern_QXL
 * @Version 1.0
 */
@Slf4j
public class BuildPo {

    /**
     * 创建bean类
     * @param sqlTableInfo
     */
    public static void execute(SqlTableInfo sqlTableInfo){
        // 判断 PACKAGE_PO 目录是否存在
        boolean exist = FileUtil.exist(DaoConstants.PACKAGE_PODIR);
        if(!exist){
            // 如果  PACKAGE_PO 目录不存在则创建目录  -> PACKAGE_PO 如下
            // E:/Code/IDEA/Campus_JavaProduceV1.0/JavaProduce_V1.0_Demo/src/main/java/com/kuang/entity/po
            FileUtil.mkdir(DaoConstants.PACKAGE_PODIR);
        }
        File BeanFile = FileUtil.file(DaoConstants.PACKAGE_PODIR.concat(sqlTableInfo.getBeanName()).concat(".java"));
        // E:\Code\IDEA\Campus_JavaProduceV1.0\JavaProduce_V1.0_Demo\src\main\java\com\kuang\entity\po\Springboot_user.java
        log.info("BeanFile:{} " + BeanFile);



        // 使用输入流写入内容
        OutputStreamWriter outputStreamWriter = null;
        BufferedWriter bufferedWriter = null;
        try {
            outputStreamWriter = new OutputStreamWriter(new FileOutputStream(BeanFile),"UTF-8");
            bufferedWriter = new BufferedWriter(outputStreamWriter);
            bufferedWriter.write("package ".concat(DaoConstants.PACKAGE_BASE).concat(".").concat(DaoConstants.PACKAGE_PO).concat(";").replace("/","."));
            bufferedWriter.newLine();
            bufferedWriter.write("import java.io.Serializable;");
            bufferedWriter.newLine();
            bufferedWriter.write("public class  " + sqlTableInfo.getBeanName() + "  implements Serializable {");
            bufferedWriter.newLine();
            bufferedWriter.write("}");
            bufferedWriter.flush();
        } catch (Exception e) {
            log.info("写入bean失败:{}",e);
        }finally {
            IoUtil.close(bufferedWriter);
            IoUtil.close(outputStreamWriter);
        }

    }


}
























